package com.carcontrol.myplugin3;

import android.app.Activity;
import android.content.Intent;
import android.content.res.Resources;
import android.os.Bundle;
import android.util.Log;

import com.hongx.pluginstand.PayInterfaceActivity;

import java.lang.reflect.Constructor;


/**
 * @author: fuchenming
 * @create: 2019-08-09 09:28
 */
public class ProxyActivity extends Activity {
    //需要加载插件的全类名
    private String className;
    PayInterfaceActivity payInterfaceActivity;

    @Override
    protected void onCreate( Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        className = getIntent().getStringExtra("className");

        Log.e("p_pa===onCreate", "==="+className);
        try {

            Class<?> aClass = getClassLoader().loadClass(className);
            Log.e("p_pa===onCreate", "2==="+aClass);
            Constructor constructor = aClass.getConstructor(new Class[]{});


//            Constructor constructor = Class.forName(className).getConstructor(new Class[]{});
            Log.e("p_pa===onCreate", "3==="+constructor);
            Object in = constructor.newInstance(new Object[]{});
            payInterfaceActivity = (PayInterfaceActivity) in;
            payInterfaceActivity.attach(this);

            Log.e("p_pa===onCreate", "4==="+payInterfaceActivity);

            //如果需要参数，可以使用Bundle
            Bundle bundle = new Bundle();
            payInterfaceActivity.onCreate(bundle);

            Log.e("p_pa===onCreate", "5==="+payInterfaceActivity);
        } catch (Exception e) {
            Log.e("p_pa===e", "==="+e);
        }
    }

    @Override
    public void startActivity(Intent intent) {
        String className = intent.getStringExtra("className");
        Intent intent1 = new Intent(this, ProxyActivity.class);
        intent1.putExtra("className", className);
        super.startActivity(intent1);
    }

    //重写加载类
    @Override
    public ClassLoader getClassLoader() {
        Log.e("p_getClassLoader===", super.getClassLoader()+"===");
        return super.getClassLoader();
//        return PluginManager.getInstance().getDexClassLoader()==null?super.getClassLoader():PluginManager.getInstance().getDexClassLoader();
    }

    //重写加载资源
    @Override
    public Resources getResources() {
        Log.e("p_getResources===", super.getResources()+"===");
        return super.getResources();
//        return PluginManager.getInstance().getResources()==null?super.getResources():PluginManager.getInstance().getResources();
    }

//    @Override
//    protected void onStart() {
//        super.onStart();
//        payInterfaceActivity.onStart();
//    }
//
//    @Override
//    protected void onResume() {
//        super.onResume();
//        payInterfaceActivity.onResume();
//    }
//
//    @Override
//    protected void onStop() {
//        super.onStop();
//        payInterfaceActivity.onStop();
//    }
//
//    @Override
//    protected void onDestroy() {
//        super.onDestroy();
//        payInterfaceActivity.onDestroy();
//    }
}